package cn.bellychang.service;

import cn.bellychang.model.ORDER_STATUS_ENUM;
import cn.bellychang.model.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

/**
 * @author ChangLiang
 * @date 2020/9/23
 */
@Service
public class OrderService {

    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    public void processOrderWithThreadSleep(Order order) {
        LOGGER.info("接到下单请求，order: {}", order);
        // mock process
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        order.setStatus(ORDER_STATUS_ENUM.completed.name());
        LOGGER.info("下单请求处理完毕, order:{}", order);
    }

    public void processOrder(Order order) {
        LOGGER.info("接到下单请求，order: {}", order);
        // mock time-consumed process and can't be interrupted
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        int loopResult = 0;
        for (long j = 0; j < 100000000L; j++) {
            loopResult += j;
        }
//        int i = 1/0;
        stopWatch.stop();
        LOGGER.warn("time consume:{} milliseconds", stopWatch.getTotalTimeMillis());
        order.setStatus(ORDER_STATUS_ENUM.completed.name());
        LOGGER.info("下单请求处理完毕, order:{}", order);
    }
}
